<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Products.php 135 2009-05-24 15:23:18Z movoin $
 */

/**
 * Controller_Products “产品中心”控制器
 *
 * @package Site
 * @subpackage Front
 *
 */

//{{include
FLEA::loadClass('Controller_Base');
//}}

class Controller_Products extends Controller_Base
{
    var $modelProducts;

    /**
     * 构造函数
     *
     * @return Controller_Products
     */
    function Controller_Products()
    {
        // 构造父类
        parent::Controller_Base();
        $this->modelProducts =& FLEA::getSingleton( 'Model_Products' );
    }

    /**
     * 显示控制台
     *
     */
    function actionIndex()
    {
        //缓存名称
        $cache_id = 'products.class.products';
        //从缓存中读取数据
        $data = FLEA::getCache($cache_id, $this->lifetime, true, true);
        if(!is_array($data)) {
            $data['rows'] = $this->modelProducts->getCategories('products', 0, 'cate_id,name', 'attachments');
            FLEA::writeCache( $cache_id, $data, true );
        }
        $this->_executeView('products', $data);
    }

    function actionList()
    {
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $classid = (int)$_GET['class'];
            $pagesize = 9;
        //缓存名称
        $cache_id = 'products.list.class' . $classid . '.page' . $page;
        //从缓存中读取数据
        $data = FLEA::getCache($cache_id, $this->lifetime, true, true);
        if(!is_array($data)) {
            $cate = $this->modelProducts->getCategory($classid, 'name');
            $data['cate'] = $cate['name'];
            $table = & $this->modelProducts->getTable();
            $table->enableLinks(array('attachments'));
            $where = array(
                array( 'col_key', 'products' ),
                array( 'site_id', 0 )
                );
            if ($classid)
                $where[] = array('cate_id', $classid);

            FLEA::loadHelper( 'pager' );
            $pager =& new FLEA_Helper_Pager($table, $page, $pagesize, $where, 'sort_id ASC, id DESC');
            $data['pager'] = $pager->getPagerData();
            $data['rows'] = $pager->findAll('id,title');
            FLEA::writeCache( $cache_id, $data, true );
        }
        $this->_executeView('products-list', $data);
    }

    function actionView()
    {
        $pro_id = (int)$_GET['pro'];
        //缓存名称
        $cache_id = 'products.view.pro' . $pro_id;
        //从缓存中读取数据
        $data = FLEA::getCache($cache_id, $this->lifetime, true, true);
        if(!is_array($data)) {
            $data = $this->modelProducts->getContent($pro_id, 'id,cate_id,title,content', 'category,attachments');
            FLEA::writeCache( $cache_id, $data, true );
        }
        $this->_executeView('products-view', $data);
    }

}
